/* Copyright 2001,2002,2003 NAH6 BV
 * All Rights Reserved
 *
 *  $Header: /var/lib/cvs/secphone/ui/phone/ConfigManager.cpp,v 1.8 2003/11/21 16:12:58 itsme Exp $
 *
 *
 * this module manages what configuration items are available
 * in what contexts.
 */

#include "CPhone.h"         // first, to avoid 'new' error with stl

#include "ConfigManager.h"
#include "CPhoneDlg.h"
#include "SpipcHandler.h"

#include "PhoneOptions.h"

#include "phone.h"          // - from machine, for cfgtype constants

#include "spif.h"           // for SPIPCPhoneDeviceName

#include "stringutils.h"    // for IntegerStringMap


// todo: this function should not be public, the dlg::SetCfgType
// function should directly take the string.
char *CFGPhoneDeviceName(int cfgtype)
{
    static char sbuf[20];
    switch(cfgtype)
    {
case CFGTYPE_GSMTAPI:         return "gsm";
case CFGTYPE_DESKTOP:         return "modem";
case CFGTYPE_GSMHAYES_COM2:   return "gsmhayes";
case CFGTYPE_POTSHAYES_COM1:  return "pots";
    default:
        sprintf(sbuf, "CFGTYPE_%02x", cfgtype);
        return sbuf;
    }
}


ConfigManager::ConfigManager()
{
    m_nCfgType= g_options.line_type;

    if (cfgSupportsDigital())
        m_nCallType= CALLTYPE_DIGITAL;
    else
        m_nCallType= CALLTYPE_ANALOG;

//  theApp.m_dlg->SetCallType( m_nCallType ); -- todo
//  theApp.m_dlg->SetCfgType( m_nCfgType );
}
void ConfigManager::Init()
{
    if (theApp.m_ipc)
        theApp.m_ipc->SetConfiguration(m_nCfgType);
}

bool ConfigManager::NextCallType()
{
    if( m_nCallType==CALLTYPE_DIGITAL )
    {
        if (!cfgSupportsAnalog())
            return false;
        m_nCallType= CALLTYPE_ANALOG;
    }
    else
    {
        if (!cfgSupportsDigital())
            return false;
        m_nCallType= CALLTYPE_DIGITAL;
    }
    //theApp.m_dlg->SetCallType( m_nCallType ); -- todo
    return true;
}

bool ConfigManager::NextCfgType()
{
#ifdef _WIN32_WCE

    if (m_nCfgType==CFGTYPE_GSMTAPI)
        m_nCfgType=CFGTYPE_POTSHAYES_COM1;
    else if (m_nCfgType==CFGTYPE_POTSHAYES_COM1)
        m_nCfgType=CFGTYPE_GSMTAPI;
    else
        m_nCfgType=CFGTYPE_GSMTAPI;

    switch(m_nCfgType)
    {
case CFGTYPE_GSMTAPI:
case CFGTYPE_GSMHAYES_COM2:
        m_nCallType= CALLTYPE_DIGITAL;
        break;
case CFGTYPE_POTSHAYES_COM1:
        m_nCallType= CALLTYPE_ANALOG;
        break;
    }
#else
    m_nCfgType= CFGTYPE_DESKTOP;
    m_nCallType= CALLTYPE_ANALOG;
#endif

    //theApp.m_dlg->SetCallType( m_nCallType ); -- todo
    //theApp.m_dlg->SetCfgType( m_nCfgType );

    theApp.m_ipc->SetConfiguration(m_nCfgType);
    return true;
}

IntegerStringMap ConfigManager::GetConfigList()
{
    IntegerStringMap map;
#ifdef _WIN32_WCE
    map[CFGTYPE_GSMTAPI]= CFGPhoneDeviceName(CFGTYPE_GSMTAPI);
    map[CFGTYPE_POTSHAYES_COM1]= CFGPhoneDeviceName(CFGTYPE_POTSHAYES_COM1);
#else
    map[CFGTYPE_DESKTOP]= CFGPhoneDeviceName(CFGTYPE_DESKTOP);
#endif
    return map;
}
bool ConfigManager::cfgSupportsAnalog()
{
    return true;
}
bool ConfigManager::cfgSupportsDigital()
{
    return (m_nCfgType==CFGTYPE_GSMTAPI || m_nCfgType==CFGTYPE_GSMHAYES_COM2);
}

IntegerStringMap ConfigManager::GetModemPorts()
{
    IntegerStringMap map;
    map[0]= "COM1";
    map[1]= "COM2";

    return map;
}
IntegerStringMap ConfigManager::GetModemTypes()
{
    IntegerStringMap map;

    map[K_HAYES_MODEMTYPE_POTS]= "Generic";
    map[K_HAYES_MODEMTYPE_POTS_GAL]= "Hayes GAL";
    map[K_HAYES_MODEMTYPE_POTS_CIR]= "Hayes CIR";
    map[K_HAYES_MODEMTYPE_POTS_ELS]= "Hayes EIS";
    map[K_HAYES_MODEMTYPE_POTS_USR]= "Hayes USRobotics";
#ifndef _WIN32_WCE
    map[K_HAYES_MODEMTYPE_GSM]= "GSM Generic";
    map[K_HAYES_MODEMTYPE_GSM_NOK]= "GSM Nokia";
    map[K_HAYES_MODEMTYPE_GSM_SIE]= "GSM Siemens";
#endif

    return map;
}
IntegerStringMap ConfigManager::GetCallTypes()
{
    IntegerStringMap map;

    map[CALLTYPE_DIGITAL]= "gsm";
    map[CALLTYPE_ANALOG]= "modem";
    //map[CALLTYPE_IP]= "ip";

    return map;
}
